package com.jsle.financial.action;

import java.text.SimpleDateFormat;
import java.util.List;

import com.jsle.financial.domain.*;

public class ControllerHelper {
	
	/**
	 * 传入公司信息集合获取{},{}格式json串
	 * @param cps
	 * @return
	 */
	public static String getCompanysDataJsonByList(List<Company> cps){
		StringBuffer sb = new StringBuffer();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for(Company cp : cps){
			sb.append("{");
			sb.append("\"id\"").append(":").append(cp.getId()).append(",");
			sb.append("\"companyCode\"").append(":\"").append(cp.getCompanyCode()).append("\",");
			sb.append("\"companyName\"").append(":\"").append(cp.getCompanyName()).append("\",");
			sb.append("\"createTime\"").append(":\"").append(sdf.format(cp.getCreateTime())).append("\",");
			sb.append("\"updateTime\"").append(":\"").append(sdf.format(cp.getUpdateTime())).append("\",");
			sb.append("\"isActive\"").append(":").append(cp.getIsActive());
			sb.append("},");
		}
		
		return removeStringBufferEndChar(sb.toString());
	}
	
	/**
	 * 传入用户组信息集合获取{},{}格式json串
	 * @param cps
	 * @return
	 */
	public static String getUserGroupsDataJsonByList(List<UserGroup> ugs){
		StringBuffer sb = new StringBuffer();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for(UserGroup ug : ugs){
			sb.append("{");
			sb.append("\"id\"").append(":").append(ug.getId()).append(",");
			sb.append("\"groupName\"").append(":\"").append(ug.getGroupName()).append("\",");
			sb.append("\"companyCode\"").append(":\"").append(ug.getCompanyCode()).append("\",");
			sb.append("\"companyName\"").append(":\"").append(ug.getCompanyName()).append("\",");
			sb.append("\"createTime\"").append(":\"").append(sdf.format(ug.getCreateTime())).append("\",");
			sb.append("\"updateTime\"").append(":\"").append(sdf.format(ug.getUpdateTime())).append("\",");
			sb.append("\"isActive\"").append(":").append(ug.getIsActive());
			sb.append("},");
		}
		
		return removeStringBufferEndChar(sb.toString());
	}
	
	/**
	 * 传入用户信息集合获取{},{}格式json串
	 * @param cps
	 * @return
	 */
	public static String getUsersDataJsonByList(List<User> users){
		StringBuffer sb = new StringBuffer();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for(User user : users){
			sb.append("{").append("\"id\"").append(":\"").append(user.getId()).append("\",");
			sb.append("\"loginName\"").append(":\"").append(user.getLoginName()).append("\",");
			sb.append("\"loginPassword\"").append(":\"").append(user.getLoginPassword()).append("\",");
			sb.append("\"cNName\"").append(":\"").append(user.getcNName()).append("\",");
			sb.append("\"positionCode\"").append(":\"").append(user.getPositionCode()).append("\",");
			sb.append("\"positionName\"").append(":\"").append(user.getPositionName()).append("\",");
			sb.append("\"createTime\"").append(":\"").append(sdf.format(user.getCreateTime())).append("\",");
			sb.append("\"updateTime\"").append(":\"").append(sdf.format(user.getUpdateTime())).append("\",");
			sb.append("\"companyCode\"").append(":\"").append(user.getCompanyCode()).append("\",");
			sb.append("\"companyName\"").append(":\"").append(user.getCompanyName()).append("\",");
			sb.append("\"userType\"").append(":\"").append(user.getUserType()).append("\",");
			sb.append("\"isActive\"").append(":\"").append(user.getIsActive()).append("\"}").append(",");
		}
		
		return removeStringBufferEndChar(sb.toString());
	}
	
	/**
	 * 传入用户-用户组关联信息集合获取{},{}格式json串
	 * @param cps
	 * @return
	 */
	public static String getUserUserGroupsDataJsonByList(List<User_UserGroups> uugs){
		StringBuffer sb = new StringBuffer();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for(User_UserGroups uug : uugs){
			sb.append("{").append("\"id\"").append(":\"").append(uug.getId()).append("\",");
			sb.append("\"userId\"").append(":\"").append(uug.getUserId()).append("\",");
			sb.append("\"userGroupId\"").append(":\"").append(uug.getUserGroupId()).append("\"}").append(",");
		}
		
		return removeStringBufferEndChar(sb.toString());
	}
	
	/**
	 * 去除String最后一个字符
	 * @param sb
	 * @return
	 */
	public static String removeStringBufferEndChar(String str){
		if(str != null && !str.equals("")){
			if(str.endsWith(",")){
				str = str.substring(0,str.length()-1);
			}
		}
		return str;
	}
	
	/**
	 * 传入资源信息集合获取{},{}格式json串
	 * @param cps
	 * @return
	 */
	public static String getResourceDataJsonByList(List<Resources> resources){
		StringBuffer sb = new StringBuffer();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for(Resources resource : resources){
			sb.append("{").append("\"id\"").append(":\"").append(resource.getId()).append("\",");
			sb.append("\"ResourceCode\"").append(":\"").append(resource.getResourceCode()).append("\",");
			sb.append("\"ResourceName\"").append(":\"").append(resource.getResourceName()).append("\",");
			sb.append("\"ResourceUrl\"").append(":\"").append(resource.getResourceUrl()).append("\",");
			sb.append("\"createTime\"").append(":\"").append(sdf.format(resource.getCreateTime())).append("\",");
			sb.append("\"updateTime\"").append(":\"").append(sdf.format(resource.getUpdateTime())).append("\",");
			sb.append("\"isActive\"").append(":\"").append(resource.getIsActive()).append("\"}").append(",");
		}
		return removeStringBufferEndChar(sb.toString());
	}
	
	/**
	 * 传入公司信息获取{},{}格式json串
	 * @param cps
	 * @return
	 */
	public static String getCompanysResDataJsonByList(Company cp){
		StringBuffer sb = new StringBuffer();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		sb.append("{");
		sb.append("\"id\"").append(":").append(cp.getId()).append(",");
		sb.append("\"companyCode\"").append(":\"").append(cp.getCompanyCode()).append("\",");
		sb.append("\"companyName\"").append(":\"").append(cp.getCompanyName()).append("\",");
		sb.append("\"createTime\"").append(":\"").append(sdf.format(cp.getCreateTime())).append("\",");
		sb.append("\"updateTime\"").append(":\"").append(sdf.format(cp.getUpdateTime())).append("\",");
		sb.append("\"isActive\"").append(":").append(cp.getIsActive()).append(",");
		sb.append("crs:[");
		StringBuffer sb2 = new StringBuffer();
		for (Company_Resources cr : cp.getCrs()) {
			sb2.append("{");
			sb2.append("\"id\"").append(":").append(cr.getId()).append(",");
			sb2.append("\"companyId\"").append(":").append(cr.getCompanyId()).append(",");
			sb2.append("\"resourceId\"").append(":").append(cr.getResourceId()).append(",");
			sb2.append("},");
		}
		sb.append(removeStringBufferEndChar(sb2.toString()));
		sb.append("]");
		sb.append("}");
		return removeStringBufferEndChar(sb.toString());
	}
	
	/**
	 * 传入资源信息获取{},{}格式json串
	 * @param cps
	 * @return
	 */
	public static String getResourcesResDataJsonByList(Resources rs){
		StringBuffer sb = new StringBuffer();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		sb.append("{");
		sb.append("\"id\"").append(":").append(rs.getId()).append(",");
		sb.append("\"resourceCode\"").append(":\"").append(rs.getResourceCode()).append("\",");
		sb.append("\"resourceName\"").append(":\"").append(rs.getResourceName()).append("\",");
		sb.append("\"resourceUrl\"").append(":\"").append(rs.getResourceUrl()).append("\",");
		sb.append("\"createTime\"").append(":\"").append(sdf.format(rs.getCreateTime())).append("\",");
		sb.append("\"updateTime\"").append(":\"").append(sdf.format(rs.getUpdateTime())).append("\",");
		sb.append("\"isActive\"").append(":").append(rs.getIsActive());
		sb.append("}");
		return removeStringBufferEndChar(sb.toString());
	}
}
